package edu.cqie.smartfarm.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.cqie.smartfarm.constant.CommonConstant;
import edu.cqie.smartfarm.constant.MessageConstant;
import edu.cqie.smartfarm.dto.BarnDTO;
import edu.cqie.smartfarm.dto.BarnQueryDTO;
import edu.cqie.smartfarm.dto.BarnUpdateDTO;
import edu.cqie.smartfarm.entity.Barn;
import edu.cqie.smartfarm.entity.Farm;
import edu.cqie.smartfarm.mapper.BarnMapper;
import edu.cqie.smartfarm.mapper.FarmMapper;
import edu.cqie.smartfarm.result.PageVO;
import edu.cqie.smartfarm.service.IBarnService;
import edu.cqie.smartfarm.utils.ThreadLocalUtil;
import edu.cqie.smartfarm.vo.BarnVO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yyw
 * @since 2024-12-26
 */
@Service
@RequiredArgsConstructor
public class BarnServiceImpl extends ServiceImpl<BarnMapper, Barn> implements IBarnService {
    private final BarnMapper barnMapper;
    private final FarmMapper farmMapper;
    /**
     * 分页查询
     * @param barnQueryDTO
     * @return
     */
    @Override
    public PageVO<BarnVO> getList(BarnQueryDTO barnQueryDTO) {
        Page<Barn> page = new Page<>(barnQueryDTO.getPageNumber(), barnQueryDTO.getPageSize());
        LambdaQueryWrapper<Barn> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .like(barnQueryDTO.getBarnName()!=null && barnQueryDTO.getBarnName().length() > 0, Barn::getBarnName, barnQueryDTO.getBarnName())
                .like(barnQueryDTO.getFarmName()!=null && barnQueryDTO.getFarmName().length() > 0, Barn::getFarmName, barnQueryDTO.getFarmName())
                .eq(barnQueryDTO.getStatus()!=null, Barn::getStatus, barnQueryDTO.getStatus());
        Page<Barn> barnPage = page(page, queryWrapper);
        List<BarnVO> barnVOList = BeanUtil.copyToList(barnPage.getRecords(), BarnVO.class);

        PageVO<BarnVO> pageVO = new PageVO<>();
        pageVO.setCurrent(barnPage.getCurrent());
        pageVO.setSize(barnPage.getSize());
        pageVO.setTotal(barnPage.getTotal());
        pageVO.setPages(barnPage.getPages());
        pageVO.setRecords(barnVOList);
        return pageVO;
    }

    @Override
    public void addBarn(BarnDTO barnDTO) {
        // 1. 判断在同一个牧场下是否重复
        LambdaQueryWrapper<Barn> queryWrapper = new LambdaQueryWrapper<Barn>()
                .eq(Barn::getFarmId, barnDTO.getFarmId())
                .eq(Barn::getBarnName, barnDTO.getBarnName());
        if (count(queryWrapper) > 0) {
            throw new RuntimeException(MessageConstant.BARN_EXISTS);
        }
        // 2.查询牧场是否存在
        Farm farm = farmMapper.selectById(barnDTO.getFarmId());
        if (farm == null) {
            throw new RuntimeException(MessageConstant.FARM_NOT_EXISTS);
        }
        // 设置牧场名称
        Barn barn = BeanUtil.copyProperties(barnDTO, Barn.class);
        barn.setFarmName(farm.getFarmName());

        // 获取当前用户id
        Map<String, Object> map = ThreadLocalUtil.get();
        barn.setUserId((Integer) map.get(CommonConstant.USER_ID));

        // 新增到数据库
        save(barn);
    }

    /**
     * 更新
     * @param barnUpdateDTO
     */
    @Override
    public void updateBarn(BarnUpdateDTO barnUpdateDTO) {
        // 判断是否存在
        Barn barn = getById(barnUpdateDTO.getBarnId());
        if (barn == null) {
            throw new RuntimeException(MessageConstant.BARN_NOT_EXISTS);
        }
        // 修改名称时不能重复
        if (barnUpdateDTO.getBarnName() != null){
            LambdaQueryWrapper<Barn> queryWrapper = new LambdaQueryWrapper<Barn>()
                    .eq(Barn::getFarmId, barn.getFarmId())
                    .eq(Barn::getBarnName, barnUpdateDTO.getBarnName());
            if (count(queryWrapper) > 0) {
                throw new RuntimeException(MessageConstant.BARN_EXISTS);
            }
        }

        LambdaUpdateWrapper<Barn> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper
                .set(barnUpdateDTO.getBarnName() != null && barnUpdateDTO.getBarnName().length() > 0,Barn::getBarnName, barnUpdateDTO.getBarnName())
                .set(barnUpdateDTO.getStatus() != null,Barn::getStatus, barnUpdateDTO.getStatus())
                .eq(Barn::getId, barnUpdateDTO.getBarnId());
        update(updateWrapper);
    }
}
